package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }


    @Override
    public Map<String,Object> getListInventory(Integer page, Integer rows, String codeOrName,Integer goodsTypeId) {
        Integer total = null;
        List<Goods> list = null;

        if (goodsTypeId != null){
            List<Integer> goodsTypeIds = goodsTypeDao.getChildrenId(goodsTypeId);
            System.out.println(goodsTypeIds);
            if (goodsTypeIds.size() == 0){
                goodsTypeIds.add(goodsTypeId);
            }
            list = goodsDao.getGoodsPageInfoBy(page,rows,codeOrName,goodsTypeIds.toArray());
            total = goodsDao.getCountTotal(codeOrName,goodsTypeIds.toArray());
        }else{
            list = goodsDao.getGoodsPageInfoBy(page,rows,codeOrName,null);
            total = goodsDao.getCountTotal(codeOrName,null);
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> getUnit() {

        HashMap<String, Object> map = new HashMap<>();
        List<Unit> list = goodsDao.getUnitList();
        map.put("rows",list);
        return map;
    }

    @Override
    public void saveOrUpdate(Goods goods, Integer goodsId) {
        if (goodsId == null){
            //添加商品
            goodsDao.saveGoods(goods);
        }else{
            //修改商品
            goodsDao.updateGoods(goods,goodsId);
        }
    }

    @Override
    public void deleteGoods(Integer goodsId) {
        goodsDao.deleteGoodsByGoodsId(goodsId);
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer total = goodsDao.getNoInventoryQuantityCountTotal(nameOrCode);
        List<Goods> list = goodsDao.getGoodsPageInfoByNoInventoryQuantity(page, rows, nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Integer total = goodsDao.getHasInventoryQuantityCountTotal(nameOrCode);
        List<Goods> list = goodsDao.getGoodsPageInfoByHasInventoryQuantity(page, rows, nameOrCode);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @Override
    public void updateInventoryQuantityByGoodsId(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.updateInventoryQuantityByGoodsId(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public Integer deleteGoodsByGoodsId(Integer goodsId) {
        Integer state = goodsDao.getStateByGoodsId(goodsId);
        if (state == 2){
            return 200;
        }else {
            goodsDao.deleteInventoryQuantityByGoodsId(goodsId);
            return 100;
        }
    }

    @Override
    public Map<String, Object> getListAlarm() {
        List<Goods> list = goodsDao.getListAlarm();
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

}
